package com.zmn.plat.admin.controller.channel.workorder.picture;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.dto.channel.ChannelWorkOrderPictureStandardDTO;
import com.zmn.plat.model.entity.channel.workorder.picture.ChannelWorkOrderPicture;
import com.zmn.plat.model.entity.channel.workorder.picture.ChannelWorkOrderPictureStandard;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 渠道-工单照片
 *
 * @author linfeng
 * @since 2020/09/17 17:29
 */
@RestController
@RequestMapping("/channel/workorder/picture/")
public class ChannelWorkOrderPictureModifyController extends ChannelWorkOrderPictureBaseController {

    /**
     * 新增修改
     *
     * @param channelWorkOrderPicture
     * @author linfeng
     * @since 2020-09-16 16:54:03
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/channel/workorder/picture/addEdit.action")
    public ResponseDTO addEdit(@RequestBody ChannelWorkOrderPicture channelWorkOrderPicture) {
        Date now = DateUtil.getNow();

        Integer id = channelWorkOrderPicture.getRelateId();
        try {
            if (id == null || id == 0) {
                channelWorkOrderPicture.setCreater(getMcStaffRealName());
                channelWorkOrderPicture.setCreateTime(now);
                channelWorkOrderPicture.setUpdater(getMcStaffRealName());
                channelWorkOrderPicture.setUpdateTime(now);
                Integer maxSortValue = Optional.ofNullable(channelWorkOrderPictureService.findMaxSort()).orElse(0);
                channelWorkOrderPicture.setSort(++maxSortValue);
                channelWorkOrderPictureService.insert(channelWorkOrderPicture);
            } else {
                channelWorkOrderPicture.setUpdater(getMcStaffRealName());
                channelWorkOrderPicture.setUpdateTime(now);
                channelWorkOrderPictureService.updateByKey(channelWorkOrderPicture);
            }

            return ResponseDTO.success(true, "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("已存在同一渠道同一照片项目同一业务类型的数据！", ex);
            return ResponseDTO.fail("已存在同一渠道同一照片项目同一业务类型的数据！");
        } catch (Exception e) {
            logger.error("新增/修改渠道-工单照片失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 新增修改
     *
     * @param channelWorkOrderPictureStandardDTO
     * @author sunlife
     * @since 2021-10-29 16:54:03
     */
    @RequestMapping(value = "save/auditDetection")
    public ResponseDTO auditDetection(@RequestBody ChannelWorkOrderPictureStandardDTO channelWorkOrderPictureStandardDTO) {
        Date now = DateUtil.getNow();
        String operator = getMcStaffRealName();

        Integer id = channelWorkOrderPictureStandardDTO.getRelateId();
        try {

            // 更新relate表信息（是否配置了标准）
            ChannelWorkOrderPicture channelWorkOrderPicture = new ChannelWorkOrderPicture();
            channelWorkOrderPicture.setRelateId(id);
            channelWorkOrderPicture.setAuditStandardFlag(CollectionUtil.isNullOrEmpty(channelWorkOrderPictureStandardDTO.getStandardList()) ? GlobalConsts.NO : GlobalConsts.YES);
            channelWorkOrderPicture.setUpdater(operator);
            channelWorkOrderPicture.setUpdateTime(now);
            channelWorkOrderPictureService.updateByKey(channelWorkOrderPicture);

            // 更新标准信息（先删除，后添加）
            ChannelWorkOrderPictureStandard query = new ChannelWorkOrderPictureStandard();
            query.setRelateId(id);
            channelWorkOrderPictureStandardService.delete(query);

            if (CollectionUtil.isNotNullOrEmpty(channelWorkOrderPictureStandardDTO.getStandardList())) {
                List<ChannelWorkOrderPictureStandard> standardList = channelWorkOrderPictureStandardDTO.getStandardList();
                for (ChannelWorkOrderPictureStandard channelWorkOrderPictureStandard : standardList) {
                    channelWorkOrderPictureStandard.setRelateId(id);

                    channelWorkOrderPictureStandard.setCreater(operator);
                    channelWorkOrderPictureStandard.setCreateTime(now);
                    channelWorkOrderPictureStandard.setUpdater(operator);
                    channelWorkOrderPictureStandard.setUpdateTime(now);
                }
                channelWorkOrderPictureStandardService.batchInsert(standardList);
            }

            return ResponseDTO.success(true, "操作成功！");
        } catch (Exception e) {
            logger.error("新增/修改渠道-工单照片失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     * @author linfeng
     * @since 2020-09-16 16:54:03
     */
    @RequestMapping(value = "updateSort")
    @RequiresPermissions("/channel/workorder/picture/list.action")
    public ResponseDTO updateSort(@RequestParam(value = "id", defaultValue = "0") Integer id,
                                  @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }

        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }

        channelWorkOrderPictureService.updateSortByKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 修改后台审核标志
     *
     * @param id
     * @param auditFlag
     * @author linfeng
     * @since 2020-09-16 16:54:03
     */
    @RequestMapping(value = "updateAuditFlag")
    @RequiresPermissions("/channel/workorder/picture/updateAuditFlag.action")
    public ResponseDTO updateAuditFlag(@RequestParam(value = "id", defaultValue = "0") Integer id,
                                       @RequestParam(value = "auditFlag", defaultValue = "1") Integer auditFlag) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }

        ChannelWorkOrderPicture channelWorkOrderPicture = new ChannelWorkOrderPicture();
        channelWorkOrderPicture.setRelateId(id);
        channelWorkOrderPicture.setAuditFlag(auditFlag);
        channelWorkOrderPicture.setUpdater(getMcStaffRealName());
        channelWorkOrderPicture.setUpdateTime(DateUtil.getNow());
        channelWorkOrderPictureService.updateAuditFlagByKey(channelWorkOrderPicture);

        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 修改自动审核标志
     *
     * @param id
     * @param auditFlag
     * @author sunlife
     * @since 2021-11-02 16:54:03
     */
    @RequestMapping(value = "updateAutoAuditFlag")
    @RequiresPermissions("/channel/workorder/picture/updateAutoAuditFlag.action")
    public ResponseDTO updateAutoAuditFlag(@RequestParam(value = "id", defaultValue = "0") Integer id,
                                       @RequestParam(value = "autoAuditFlag", defaultValue = "1") Integer autoAuditFlag) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        System.out.println(id);
        System.out.println(autoAuditFlag);

        ChannelWorkOrderPicture channelWorkOrderPicture = new ChannelWorkOrderPicture();
        channelWorkOrderPicture.setRelateId(id);
        channelWorkOrderPicture.setAutoAuditFlag(autoAuditFlag);
        channelWorkOrderPicture.setUpdater(getMcStaffRealName());
        channelWorkOrderPicture.setUpdateTime(DateUtil.getNow());
        channelWorkOrderPictureService.updateAutoAuditFlagByKey(channelWorkOrderPicture);

        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 修改必传标志
     *
     * @param id
     * @param requiredFlag
     * @author linfeng
     * @since 2020-09-16 16:54:03
     */
    @RequestMapping(value = "updateRequiredFlag")
    @RequiresPermissions("/channel/workorder/picture/updateRequiredFlag.action")
    public ResponseDTO updateRequiredFlag(@RequestParam(value = "id", defaultValue = "0") Integer id,
                                          @RequestParam(value = "requiredFlag", defaultValue = "1") Integer requiredFlag) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }

        ChannelWorkOrderPicture channelWorkOrderPicture = new ChannelWorkOrderPicture();
        channelWorkOrderPicture.setRelateId(id);
        channelWorkOrderPicture.setRequiredFlag(requiredFlag);
        channelWorkOrderPicture.setUpdater(getMcStaffRealName());
        channelWorkOrderPicture.setUpdateTime(DateUtil.getNow());
        channelWorkOrderPictureService.updateRequiredFlagByKey(channelWorkOrderPicture);

        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 删除
     *
     * @param id
     * @author linfeng
     * @since 2020-09-16 16:54:03
     */
    @RequestMapping(value = "delete")
    @RequiresPermissions("/channel/workorder/picture/delete.action")
    public ResponseDTO delete(@RequestParam(value = "id", defaultValue = "0") Integer id) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }

        channelWorkOrderPictureService.deleteByKey(id);

        return ResponseDTO.success(true, "操作成功！");
    }

}
